package com.telit.firecommand.service.impl;

import com.telit.common.util.JwtTokenUtils;
import com.telit.firecommand.dao.CommAlarmPromptDao;
import com.telit.firecommand.entity.CommAlarmPrompt;
import com.telit.firecommand.service.CommAlarmPromptService;
import com.telit.firecommand.vo.CommAlarmPromptVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;


/**
 * Create by Chencf 2022/10/23
 */

@Slf4j
@Service
public class CommAlarmPromptServiceImpl implements CommAlarmPromptService {

    @Autowired
    private CommAlarmPromptDao commAlarmPromptDao;

    @Override
    public void insertOrUpdateAlarmPrompt(CommAlarmPrompt dto) {
        String districtCode = JwtTokenUtils.getDistrictCode();
        String realName = JwtTokenUtils.getRealName();
        //根据id查询是否存在
        CommAlarmPrompt exist = commAlarmPromptDao.selectById(dto.getId());
        if (Objects.isNull(exist)) {
            //新增
            dto.setCreateUser(realName);
            dto.setCreateTime(new Date());
            dto.setDistrictCode(districtCode);
            commAlarmPromptDao.insertSelective(dto);
        } else {
            //修改
            CommAlarmPrompt commAlarmPrompt = new CommAlarmPrompt();
            commAlarmPrompt.setShiPrompt(dto.getShiPrompt());
            commAlarmPrompt.setShengPrompt(dto.getShengPrompt());
            commAlarmPrompt.setXiangPrompt(dto.getXiangPrompt());
            commAlarmPrompt.setUpdateTime(new Date());
            commAlarmPrompt.setUpdateUser(realName);
            commAlarmPrompt.setFilePath(dto.getFilePath());
            commAlarmPrompt.setId(dto.getId());
            commAlarmPromptDao.updateByPrimaryKeySelective(commAlarmPrompt);
        }
    }

    @Override
    public CommAlarmPromptVO getAlarmPrompt() {
        CommAlarmPromptVO alarmPrompt = commAlarmPromptDao.getAlarmPrompt();
//        String filePath = alarmPrompt.getFilePath();
        // 封装数据
        // TODO：v1.1.0 预览图片
//        MultimediaVO multimediaVO = new MultimediaVO(MinioUtils.previewImg(alarmPrompt.getFilePath()),
//                null, alarmPrompt.getCreateTime(), alarmPrompt.getCreateUser(),
//                null);
//        alarmPrompt.setMediaUrl(multimediaVO.getMediaUrl());
        return alarmPrompt;
    }

}
